package com.spillman.signtab.fragments;

import java.io.File;
import java.io.FileOutputStream;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.gesture.GestureOverlayView;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import com.spillman.signtab.R;
import com.spillman.signtab.utils.FragmentCallbacks;

public class SignatureDialogFragment extends DialogFragment {

	private static final String TITLE_STRING_ID = "TitleStringId";
	private static final String MESSAGE_STRING_ID = "MessageStringId";
	private static final String MESSAGE_STRING_PLAIN = "MessageStringPlain";
	private GestureOverlayView gestureView;
	private static final String SAVE_DIR = Environment.getExternalStorageDirectory() + "/dcim/SignTab/";

	public static SignatureDialogFragment newInstance(int titleStringId, int messageStringId) {
		SignatureDialogFragment dialogFragment = new SignatureDialogFragment();

		Bundle args = new Bundle();
		args.putInt(TITLE_STRING_ID, titleStringId);
		args.putInt(MESSAGE_STRING_ID, messageStringId);
		dialogFragment.setArguments(args);

		return dialogFragment;
	}

	public static SignatureDialogFragment newInstance(int titleStringId, String message) {
		SignatureDialogFragment dialogFragment = new SignatureDialogFragment();

		Bundle args = new Bundle();
		args.putInt(TITLE_STRING_ID, titleStringId);
		args.putString(MESSAGE_STRING_PLAIN, message);
		dialogFragment.setArguments(args);

		return dialogFragment;
	}

	@Override
	public Dialog onCreateDialog(Bundle icicle) {
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setTitle(getArguments().getInt(TITLE_STRING_ID));

		if (getArguments().getInt(MESSAGE_STRING_ID) != 0) {
			builder.setMessage(getArguments().getInt(MESSAGE_STRING_ID));
		} else {
			builder.setMessage(getArguments().getString(MESSAGE_STRING_PLAIN));
		}

		LayoutInflater inflater = getActivity().getLayoutInflater();
		View rootView = inflater.inflate(R.layout.sign_fragment, null);

		gestureView = (GestureOverlayView) rootView.findViewById(R.id.signaturePad);
		builder.setView(rootView);

		builder.setPositiveButton(getString(R.string.done), new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				saveSig();
				((FragmentCallbacks) getActivity()).clear();

			}
		});

		return builder.create();
	}

	private void saveSig() {
		try {
			gestureView.setDrawingCacheEnabled(true);
			Bitmap bm = Bitmap.createBitmap(gestureView.getDrawingCache());
			File f = new File(SAVE_DIR + "signature.png");
			f.createNewFile();
			FileOutputStream os = new FileOutputStream(f);
			// compress to specified format (PNG), quality - which is ignored
			// for PNG, and out stream
			bm.compress(Bitmap.CompressFormat.PNG, 100, os);
			os.close();
		} catch (Exception e) {
			Log.v("Gestures", e.getMessage());
			e.printStackTrace();
		}
	}
}
